package com.srit.system.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.srit.system.bean.Role;
import com.srit.system.bean.UrlParent;
import com.srit.system.bean.User;
import com.srit.system.dao.RoleDao;
import com.srit.system.dao.UrlDao;
import com.srit.system.dao.UserDao;

@Controller
public class LoginController {
	//@Resource
	//private UserValidator userValidator;
	@Autowired
	private UserDao userDao;
	@Autowired
	private RoleDao roleDao;
	@Autowired
	private UrlDao urlDao;
	@RequestMapping(value = "/login")
	public String login(@ModelAttribute User user,BindingResult result,ModelMap modelMap){
		//userValidator.validate(user, result);
//      Map<String, String> map = new HashMap<String, String>();  
//      map.put("name", user.getName());  
//      map.put("password", user.getPassword());  
//      modelMap.put("user", map); 
		// if(result.hasErrors()) {  
	     //       return "system/user";  
	    // } 
		user.setUserName("admin");
		user.setPassword("123456");
		 User userVo = userDao.getUserByName(user.getUserName());
		 Role role = roleDao.getRoleByUseId(userVo.getUserId());
		 modelMap.addAttribute("role", role);
		 List<UrlParent> list = urlDao.listIsParent(role.getUrl_id().split(","));
		 modelMap.addAttribute("UrlList",list);
		 return "index";
	}
}
